﻿using Autofac;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RM.BasicLibrary.Iocs
{
    public interface IIocManager
    {
        IIocManager Register<TInterface, TInstance>();
        IIocManager Register<TInterface, TInstance>(string instanceName);
        IIocManager Register<TInstance>(TInstance instance) where TInstance : class;
        IIocManager Register<TInstance>();
        dynamic GetService(Type serviceType);
        TService GetService<TService>();
        TService GetService<TService>(string instanceName);
    }
    /// <summary>
        /// 基于Autofac的服务定位器
        /// </summary>
    public class AutofacIocManager : IIocManager
    {
        private IContainer _container = null;
        private ContainerBuilder containerBuilder = new ContainerBuilder();
        public AutofacIocManager()
        {

        }
        public void Build()
        {
            if (_container == null)
                _container = containerBuilder.Build();
        }
        public IIocManager Register<TInstance>()
        {
            containerBuilder.RegisterType<TInstance>();
            return this;
        }
        public IIocManager Register<TInterface, TInstance>()
        {
            containerBuilder.RegisterType<TInstance>().As<TInterface>();
            return this;
        }
        public IIocManager Register<TInstance>(TInstance instance) where TInstance : class
        {
            containerBuilder.RegisterInstance(instance);
            return this;
        }
        /// <summary>
        /// IPerson p = container.ResolveNamed<IPerson>("worker");
        ///  containerBuilder.RegisterType<TInterface>().Named<TInstance>(instanceName);
        /// </summary>
        /// <typeparam name="TInterface"></typeparam>
        /// <typeparam name="TInstance"></typeparam>
        /// <param name="instanceName"></param>
        /// <returns></returns>
        public IIocManager Register<TInterface, TInstance>(string instanceName)
        {
            containerBuilder.RegisterType<TInterface>().Named<TInstance>(instanceName);
            return this;
        }

        /// <summary>
        /// 设置Ico容器
        /// </summary>
        /// <param name="container"></param>
        public void SetContainer(IContainer container)
        {
            Build();
            _container = container;
        }

        /// <summary>
         /// 获取服务
         /// </summary>
         /// <param name="serviceType"></param>
         /// <returns></returns>
        public dynamic GetService(Type serviceType)
        {
            Build();
            return _container.Resolve(serviceType);
        }

        /// <summary>
        /// 获取服务
        /// </summary>
        /// <typeparam name="TService"></typeparam>
         /// <returns></returns>
        public TService GetService<TService>()
        {
            Build();
            return _container.Resolve<TService>();
        }
        /// <summary>
        /// 获取服务
        /// </summary>
        /// <typeparam name="TService"></typeparam>
         /// <returns></returns>
        public TService GetService<TService>(string instanceName)
        {
            Build();
            return _container.ResolveNamed<TService>(instanceName);
        }
        /// <summary>
                /// 获取容器对象
                /// </summary>
                /// <returns></returns>
        public IContainer GetContainer()
        {
            return _container;
        }

        public ContainerBuilder GetContainerBuilder()
        {
            return containerBuilder;
        }
    }

}
